package cn.wei.reallyEnjoy.user.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.bumptech.glide.Glide;

import cn.wei.reallyEnjoy.R;
import cn.wei.reallyEnjoy.app.LoginActivity;
import cn.wei.reallyEnjoy.base.BaseFragment;
import cn.wei.reallyEnjoy.user.activity.MyCollectionActivity;
import cn.wei.reallyEnjoy.user.activity.MyDiaryActivity;
import cn.wei.reallyEnjoy.user.activity.WriteDiaryActivity;
import cn.wei.reallyEnjoy.user.bean.User;
import cn.wei.reallyEnjoy.utils.Constants;

/**
 * 主页面的fragment
 */
public class UserFragment extends BaseFragment {

    private ImageView iv_usercenter_headimg;
    private TextView tv_user_center_nickname;
    private TextView tv_user_center_mobile;
    private RelativeLayout user_center_diary;
    private RelativeLayout user_center_collection;
    private RelativeLayout write_diary;
    private Button logout;


    @Override
    public View initView() {
        Log.e(null,"我的页面的fragment的UI被初始化");
        View view  = View.inflate(mContext, R.layout.fragment_user,null);
        iv_usercenter_headimg = view.findViewById(R.id.iv_usercenter_headimg);
        tv_user_center_nickname = view.findViewById(R.id.tv_user_center_nickname);
        tv_user_center_mobile = view.findViewById(R.id.tv_user_center_mobile);
        user_center_diary = view.findViewById(R.id.user_center_diary);
        user_center_collection = view.findViewById(R.id.user_center_collection);
        write_diary = view.findViewById(R.id.write_diary);
        logout = view.findViewById(R.id.logout);
        //设置监听事件
        initListener();
        return view;
    }


    //设置监听事件
    private void initListener() {
        user_center_diary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MyDiaryActivity.class);
                startActivity(intent);
            }
        });
        user_center_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MyCollectionActivity.class);
                startActivity(intent);
            }
        });
        write_diary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, WriteDiaryActivity.class);
                startActivity(intent);
            }
        });
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(mContext)
                        .setTitle("提示")
                        .setMessage("是否退出登录")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                                System.exit(0);//正常退出
                                //清空文件

                                SharedPreferences sp= mContext.getSharedPreferences("user",MODE_PRIVATE);

                                if(sp!=null){
                                    sp.edit().clear().commit();
                                    Intent intent=new Intent(mContext,LoginActivity.class); //使用getActivity得到当前Activity
                                    startActivity(intent);
                                    getActivity().finish(); //销毁当前Activity（即一直按back返回也不会回到前一个Activity，直接回到桌面）
                                }


                            }
                        })
                       .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                           @Override
                           public void onClick(DialogInterface dialog, int which) {

                           }
                       })
                        .show();
            }
        });


    }

    @Override
    public void initData() {
        Log.e(null,"我的页面的fragment数据的UI被初始化");
        super.initData();
        //是否登录
        isLogin();
    }

    private void isLogin() {
        SharedPreferences sp = this.getActivity().getSharedPreferences("user", MODE_PRIVATE);
        String mobile = sp.getString("mobile", "");
        System.out.println("sp:"+sp.toString());
        System.out.println("name:"+mobile);
        if (TextUtils.isEmpty(mobile)){
            //本地没有保存过登录信息，给出提示要求登录
            doLogin();
        }else{
            //已登录，加载用户并显示
            User user = readUserInfo();
            System.out.println(user.toString());
            Glide.with(this.getActivity()).load(Constants.IMG_URL+user.getHeadImg()).into(iv_usercenter_headimg);
            tv_user_center_nickname.setText(user.getNickName());
            tv_user_center_mobile.setText(user.getMobile());

        }
    }

    private User readUserInfo() {
        SharedPreferences sp = mContext.getSharedPreferences("user", MODE_PRIVATE);
        User user = new User();
        user.setId(sp.getString("id", ""));
        user.setNickName(sp.getString("nickName", ""));
        user.setMobile(sp.getString("mobile", ""));
        user.setHeadImg(sp.getString("headImg", ""));
        user.setSex(sp.getString("sex", ""));
        return user;
    }

    private void doLogin() {

        new AlertDialog.Builder(this.getActivity())
                .setTitle("提示")
                .setMessage("请先登录")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent=new Intent(mContext,LoginActivity.class); //使用getActivity得到当前Activity
                        startActivity(intent);
                        getActivity().finish(); //销毁当前Activity（即一直按back返回也不会回到前一个Activity，直接回到桌面）

                    }
                })
                .setCancelable(false)
                .show();
    }
}
